import { SysMenu } from '../../menus/entities/menu.entity';
import { SysUser } from '../../users/entities/user.entity';
import {
  Column,
  Entity,
  Index,
  JoinTable,
  ManyToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';

@Index('uk_name', ['name'], { unique: true })
@Index('uk_code', ['code'], { unique: true })
@Entity('sys_role', { schema: 'lzo_admin_nestjs' })
export class SysRole {
  @PrimaryGeneratedColumn({ type: 'bigint', name: 'id' })
  id: string;

  @Column('varchar', {
    name: 'name',
    unique: true,
    comment: '角色名称',
    length: 64,
  })
  name: string;

  @Column('varchar', {
    name: 'code',
    unique: true,
    comment: '角色编码',
    length: 32,
  })
  code: string;

  @Column('int', {
    name: 'sort',
    nullable: true,
    comment: '显示顺序',
    default: () => "'1'",
  })
  sort: number | null;

  @Column('int', {
    name: 'status',
    nullable: true,
    comment: '角色状态(1-正常 0-停用)',
    default: () => "'1'",
  })
  status: number | null;

  @Column('tinyint', {
    name: 'data_scope',
    nullable: true,
    comment: '数据权限(0-所有数据 1-部门及子部门数据 2-本部门数据3-本人数据)',
    default: () => "'3'",
  })
  dataScope: number;

  @Column('bigint', { name: 'create_by', nullable: true, comment: '创建人 ID' })
  createBy: string | null;

  @Column('datetime', {
    name: 'create_time',
    nullable: true,
    comment: '创建时间',
  })
  createTime: Date | null;

  @Column('bigint', { name: 'update_by', nullable: true, comment: '更新人ID' })
  updateBy: string | null;

  @Column('datetime', {
    name: 'update_time',
    nullable: true,
    comment: '更新时间',
  })
  updateTime: Date | null;

  @Column('int', {
    name: 'is_deleted',
    nullable: true,
    comment: '逻辑删除标识(0-未删除 1-已删除)',
    default: () => "'0'",
  })
  isDeleted: number | null;

  @Column('datetime', { name: 'deleted_at', nullable: true })
  deletedAt: Date | null;

  @ManyToMany(() => SysMenu, (sysMenu) => sysMenu.sysRoles)
  @JoinTable({
    name: 'sys_role_menu',
    joinColumns: [{ name: 'role_id', referencedColumnName: 'id' }],
    inverseJoinColumns: [{ name: 'menu_id', referencedColumnName: 'id' }],
    schema: 'lzo_admin_nestjs',
  })
  sysMenus: SysMenu[];

  @ManyToMany(() => SysUser, (sysUser) => sysUser.sysRoles)
  sysUsers: SysUser[];
}
